package joodle.tools;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import joodle.domain.Point;

/**
 * <p><center>
 * <img src="http://ug.bcc.bilkent.edu.tr/~m_ozdal/Site/Reports_files/design_files/image011.jpg" />
 * </center></p>
 */
public class ScaleTool extends TransformationTool {

	private double factor = 1.0;
	
	/**
	 * Draws intermediate figures as an indicator of ScaleTool
	 * @see Tool#drawInterim(Graphics)
	 */
	@Override
	public void drawInterim(Graphics g) {
		if(transformedShape == null)
			return;
		
		super.drawInterim(g);
		Point c = transformedShape.getCenter();
		String sf = String.format("%.1fx", factor);
		Graphics2D g2 = (Graphics2D) g.create();
		g2.setColor(INTERIM_COLOR);
		g2.drawString(sf, c.getX(), c.getY());
		g2.dispose();
	}

	/**
	 * Invoked when the mouse cursor has been moved onto a component. Listens the up and down movements
	 * of the mouse
	 * @see Tool#handleMove(Point, boolean, boolean)
	 */
	@Override
	public void handleMove(Point p, boolean ctrl, boolean lb) {
		if(!lb || currentPoint == null || transformedShape == null)
			return;
		
		float sy = 1 - (p.getY() - currentPoint.getY()) / 50.0f;
		factor *= sy;
		
		Point c = transformedShape.getCenter();
		AffineTransform at = AffineTransform.getTranslateInstance(c.getX(), c.getY());
		at.concatenate(AffineTransform.getScaleInstance(sy, sy));
		at.concatenate(AffineTransform.getTranslateInstance(-c.getX(), -c.getY()));
		currentTransform.concatenate(at);
		transformedShape.transform(at);
		currentPoint = p;
	}
	
	/**
	 * Invoked when a mouse button has been released on a component.
	 * @see Tool#handleRelease(Point, boolean)
	 */
	@Override
	public void handleRelease(Point p, boolean ctrl) {
		super.handleRelease(p, ctrl);
		factor = 1.0;
	}

}
